<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;
use AuctionCore\CarResource\Car;

/**
 * 我买(卖)的车
 *
 * @author Eric
 */
class MyCar
{

    private $myCar;

    public function __construct()
    {
        $this->myCar = M("cars");
        $this->owner = M("car_owner");
        $this->bargain = M("bargain_log");
    }

    /**
     * //获取我最后卖的车ID
     * @param $uid
     * @return mixed
     */
    public static function get_my_car_id_sold($uid)
    {
        $ret = M('car_owner')->alias('co')->field("c.car_id")->join("LEFT JOIN ".C('DB_PREFIX') . "cars c ON c.owner_id=co.owner_id")
            ->where(array('co.uid' => array('eq', $uid)))->order("co.owner_id DESC")->find();
        return $ret['car_id'];
    }

    /**
     * //获取我最后卖的车ID
     * @param $uid
     * @return mixed
     */
    public static function get_my_car_id_bought($uid)
    {
        $ret = M('custom_order')->where(array('uid' => array('eq', $uid)))->order("c_order_id DESC")->find();
        return $ret['car_id'];
    }

    /**
     * //获取我最后买到的车
     * @param $uid
     * @return mixed
     */
    public static function get_my_bought($uid)
    {
        return M('custom_order')->where("uid=" . $uid)->order("c_order_id desc")->find();

    }

    /**
     * 获取车辆详情
     * @param $car_id
     * @return array|bool
     */
    public static function get_car_detail($car_id)
    {

        if (intval($car_id) < 1) {
            return false;
        }
        $car = new Car($car_id);
        $car_info = $car->detail();

        if (empty($car_info)) {
            return false;
        }
        $car_data = array();
        $car_data['car_id'] = $car_id;
        $car_data['add_price'] = $car_info['bid_up_price'];
        $car_data['cut_price'] = $car_info['cut_down_price'];
        //图片
        $image = $car->get_pic_by_key($car_id, 'p_left_front45');
        $car_data['image'] = $image['pic_url'];
        //车型
        $cm = new \AuctionCore\CarResource\CarModel();
        $series = $cm->get_model_by_id($car_info['series_id'],'series');
        $car_data['car_name'] = $series['name'] . ' ' . $car_info['car_cc'] . ($car_info['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $car_info['gearbox']);
        //车况等级
        $car_data['composite_level'] = $car_info['composite_state'] . $car_info['accident_level'];
        //成交价
        $car_data['success_price'] = $car_info['success_price'];
        return $car_data;
    }

    /**
     * //增加抬（砍)价记录
     * @parm $data
     * @return mixed
     */
    public function add_bargain($data)
    {
        $data['createtime'] = date('Y-m-d H:i:s');
        $ret = $this->bargain->add($data);
        return $ret;
    }

    /**
     * 获取抬(砍)价记录
     *  砍价cut down
     *  抬价bid up
     * @page //当前页码
     * @size //每页显示记录条数
     * @map  //查询条件
     */
    public function get_bargain_log($page, $size, $map)
    {
        $total = $this->bargain->where($map)->count();
        $ret = $this->bargain->where($map)->limit($size)->page($page)->order('id DESC')->select();
        $list = array();
        foreach ($ret as $key => $val) {
//            $list[$key]['name'] = C('3RD.' . $val['3rd']) . '：' . $val['nickname'];
            $list[$key]['name'] = showHideMobile($val['phone']);
            $list[$key]['phone'] = $val['phone'];
            $list[$key]['price'] = '+'.$val['price'];
            $list[$key]['time'] = $val['createtime'];
        }
        return array($list, $total);
    }

    public function check_bargain($map)
    {
        return $this->bargain->where($map)->count();

    }

    /**
     * 获取可以抬价的车
     * 检测通过即可参与抬价，拍单结束即活动结束
     */
    public function get_cars_up($page,$limit,$params=array()){
            $map = array();

            //
            if($params['brand_id']){
                $map['c.brand_id'] = array('eq',$params['brand_id']);
            }
            if($params['series_id']){
                $map['c.series_id'] = array('eq',$params['series_id']);
            }
            if($params['model_id']){
                $map['c.model_id'] = array('eq',$params['model_id']);
            }
            if($params['car_no']){
                $map['c.car_no'] = array('eq',$params['car_no']);
            }
            //
            if($params['phone']){
                $map['co.phone'] = array('eq',$params['co.phone']);
            }
            if($params['come_from']){
                $map['co.comefrom'] = array('eq',$params['come_from']);
            }

            $where = array();
            if($params['status']=='0'){//进行中
                $where['c.is_game_over'] = array('eq','0');
                $where['c.car_status'] = array('neq','2');
                $where['co.business_status'] = array('not in','3,6');
                $map['_complex'] = $where;
                $where['_logic'] = 'and';
            }elseif($params['status']=='1'){//已结束
                $where['c.is_game_over'] = array('eq','1');
                $where['c.car_status'] = array('eq','2');
                $where['co.business_status'] = array('in','3,6');
                $where['_logic'] = 'or';
            }
            $map['_complex'] = $where;

            $count = M('cars')->alias('c')->join(C('DB_PREFIX')."car_owner co ON c.owner_id=co.owner_id")
                ->join(" LEFT JOIN ".C('DB_PREFIX')."order o ON o.car_id=c.car_id")
                ->where($map)->count();
            $cols = "c.car_id,c.car_no,c.car_status,c.series_id,c.car_cc,c.turbo,c.gearbox,c.bid_up_price,c.is_game_over,";
            $cols .= "co.phone,co.comefrom,co.business_status";

            $list = M('cars')->alias('c')->field($cols)->join(C('DB_PREFIX')."car_owner co ON c.owner_id=co.owner_id")
                ->where($map)->page($page)->limit($limit)->select();

            $cm = new \AuctionCore\CarResource\CarModel();
            $chanel = new \AuctionCore\Other\Channel(3);
            $arr = $chanel->get_children_channel();
            $come_from = array();
            foreach($arr as $key=>$val){
                $come_from[$val['from_id']]=$val['from_name'];
            }

            foreach($list as $key=>$val){
                $list[$key] = $val;
                //车型
                $series = $cm->get_model_by_id($val['series_id'],'series');
                $list[$key]['model_show'] = $series['name'] . ' ' . $val['car_cc'] . ($val['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $val['gearbox']);
                //渠道
                $list[$key]['come_from'] = $come_from[$val['comefrom']];
            }
            $ret = array();
            $ret['count'] = $count;
            $ret['list'] = $list;
            return $ret;
    }

    /**
     * 获取可以砍价的车
     */
    public function get_cars_down(){

    }

    /**
     *获取意向车源
     * @param $uid 用户ID
     */
    public function get_intent_cars($uid){

            $map = array();
            $map['c.uid'] = $uid;

            $cols = 'cs.sale_price,car.car_id,car.car_no,car.series_id,car.car_cc,car.turbo,car.gearbox,';
            $cols .= 'car.first_reg_date,car.pointer_mileage,car.emission,car.plate_prefix,car.plate_suffix';
            $list= M('custom_sale_intent')->alias('csi')
                ->field($cols)
                ->join(C('DB_PREFIX')."car_sale cs ON csi.sale_id=cs.sale_id")
                ->join(C('DB_PREFIX')."custom_intent ci ON ci.id=csi.intent_id")
                ->join(C('DB_PREFIX')."custom c ON c.id=ci.custom_id")
                ->join(C('DB_PREFIX')."cars car ON car.car_id=cs.car_id")
                ->where($map)->select();
            $cm = new \AuctionCore\CarResource\CarModel();
            foreach($list as $k=>$v){
                //销价
                $list[$k]['sale_price'] = format_money($v['sale_price']);
                $list[$k]['first_reg_date'] = date('Y年m月',strtotime($v['first_reg_date']));
                $list[$k]['emission'] = C('EMISSION.'.$v['emission']);
                $list[$k]['plate'] = C('PLATE_PREFIX.'.$v['plate_prefix']).substr($v['plate_suffix'],0,1);
                $list[$k]['pointer_mileage'] = round($v['pointer_mileage']/10000,1);

                //车源
                $car = new \AuctionCore\CarResource\Car($v['car_id']);
                //图片
                $image = $car->get_pic_by_key($v['car_id'], 'p_left_front45');
                $list[$k]['car_pic'] = $image['pic_url'];

                //
                //车型
                $series = $cm->get_model_by_id($v['series_id']);
                $list[$k]['model_show'] = $series['name'] . ' ' . $v['car_cc'] . ($v['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $v['gearbox']);



                //
                $c = M('custom_order')->where("uid='".$uid."'")->find();
                if($c['car_id']==$v['car_id']){
                    $list[$k]['ordered'] = true;
                }

            }
            return $list;
    }

    /**
     * 获取我买的车详情
     * @param $map
     * @return mixed
     */
    public function get_custom_car($map){
            return M('car_sale')->alias('cs')->join(C('DB_PREFIX')."cars c ON c.car_id=cs.car_id")
                ->where($map)->find();
    }

    /**
     * 获取我的订单信息
     * @param $map
     * @return mixed
     */
    public function get_custom_order($map){
            return M('custom_order')->where($map)->find();
    }
}
